home *** CD-ROM | disk | FTP | other *** search
/ Aminet 32 / Aminet 32 (1999)(Schatztruhe)[!][Aug 1999].iso / Aminet / dev / lang / Python151_Src.lha / Python1.5_Source / Amiga / getopt.c < prev    next >
C/C++ Source or Header  |  1994-10-04  |  3KB  |  90 lines

  1. RCS_ID_C="$Id: getopt.c,v 4.3 1994/10/04 07:42:11 jraja Exp $";
  2. /*
  3.  *      getopt.c - Unix compatible command line option parsing
  4.  */
  5.  
  6. /*
  7. **    @(#)getopt.c    2.5 (smail) 9/15/87
  8. */
  9.  
  10. /*
  11.  * Here's something you've all been waiting for:  the AT&T public domain
  12.  * source for getopt(3).  It is the code which was given out at the 1985
  13.  * UNIFORUM conference in Dallas.  I obtained it by electronic mail
  14.  * directly from AT&T.  The people there assure me that it is indeed
  15.  * in the public domain.
  16.  * 
  17.  * There is no manual page.  That is because the one they gave out at
  18.  * UNIFORUM was slightly different from the current System V Release 2
  19.  * manual page.  The difference apparently involved a note about the
  20.  * famous rules 5 and 6, recommending using white space between an option
  21.  * and its first argument, and not grouping options that have arguments.
  22.  * Getopt itself is currently lenient about both of these things White
  23.  * space is allowed, but not mandatory, and the last option in a group can
  24.  * have an argument.  That particular version of the man page evidently
  25.  * has no official existence, and my source at AT&T did not send a copy.
  26.  * The current SVR2 man page reflects the actual behavor of this getopt.
  27.  * However, I am not about to post a copy of anything licensed by AT&T.
  28.  */
  29.  
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <stdio.h>
  33.  
  34. /*LINTLIBRARY*/
  35. #define ERR(s, c)\
  36.   if(opterr) { fprintf(stderr, "%s%s%lc\n", argv[0], s, c); }
  37.  
  38. int    opterr = 1;
  39. int    optind = 1;
  40. int    optopt;
  41. char    *optarg;
  42.  
  43. /*
  44.  * Using the 'const' on the argv below increases code size on SAS/C 6.51 by
  45.  * 24 bytes, even when it should have no effect on the generated code!
  46.  */
  47. int
  48. getopt(int argc, char * /*const*/ argv[], char const *opts)
  49. {
  50.     static int sp = 1;
  51.     register int c;
  52.     register char *cp;
  53.  
  54.     if(sp == 1)
  55.         if(optind >= argc ||
  56.            argv[optind][0] != '-' || argv[optind][1] == '\0')
  57.             return(EOF);
  58.         else if(strcmp(argv[optind], "--") == NULL) {
  59.             optind++;
  60.             return(EOF);
  61.         }
  62.     optopt = c = argv[optind][sp];
  63.     if(c == ':' || (cp=index(opts, c)) == NULL) {
  64.         ERR(": illegal option -- ", c);
  65.         if(argv[optind][++sp] == '\0') {
  66.             optind++;
  67.             sp = 1;
  68.         }
  69.         return('?');
  70.     }
  71.     if(*++cp == ':') {
  72.         if(argv[optind][sp+1] != '\0')
  73.             optarg = &argv[optind++][sp+1];
  74.         else if(++optind >= argc) {
  75.             ERR(": option requires an argument -- ", c);
  76.             sp = 1;
  77.             return('?');
  78.         } else
  79.             optarg = argv[optind++];
  80.         sp = 1;
  81.     } else {
  82.         if(argv[optind][++sp] == '\0') {
  83.             sp = 1;
  84.             optind++;
  85.         }
  86.         optarg = NULL;
  87.     }
  88.     return(c);
  89. }
  90.